package com.eduction.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.eduction.bean.SessionUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eduction.common.ReturnCode;
import com.eduction.modules.system.entity.FileDownload;
import com.eduction.resource.BaseController;
import com.eduction.service.FileDownloadService;

@Controller
@RequestMapping("/file")
public class FileDownLoadController extends BaseController {

	
	@Autowired
	private FileDownloadService fileDownloadService;
	
	/**
	 * 保存学校的自评表
	 */
	@RequestMapping(value = "/saveFileDownload" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> saveFileDownload(HttpServletRequest request,@RequestBody Map<String ,String> map){
		String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
		if(StringUtils.isEmpty(user_type)){
			return toResponse(null,ReturnCode.FAIL,"暂无权限！");
		}

		String s_filepath = map.get("fileUrl");
		if(s_filepath == null)
			return toResponse(null, ReturnCode.FAIL, "url为空!");
		String createTime =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		String fileName=s_filepath.substring(s_filepath.lastIndexOf("/")+1, s_filepath.length());
			
		FileDownload af = new FileDownload();
		af.setS_create_time(createTime);
		af.setS_filename(fileName);
		af.setS_filepath(s_filepath);
		fileDownloadService.saveFileDownload(af);
		return toResponse(null, ReturnCode.SUCCESS, "查询成功!");
	}
}
